<?php require_once(getcwd()."/includes/session.php"); ?>
<?php require_once(getcwd()."/includes/connection.php"); ?>
<?php require_once(getcwd()."/includes/functions.php"); ?>
<?php
	
	if (logged_in()) {
		redirect_to("juke.php");
	}

	include_once(getcwd()."/includes/form_functions.php");
	
	// START FORM PROCESSING
	if (isset($_POST['submit'])) { // Form has been submitted.
		$errors = array();

		// perform validations on the form data
		$required_fields = array('username', 'password');
		$errors = array_merge($errors, check_required_fields($required_fields, $_POST));

		$fields_with_lengths = array('username' => 30, 'password' => 30);
		$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

		$username = trim(mysql_prep($_POST['username']));
		$password = trim(mysql_prep($_POST['password']));
		$hashed_password = sha1($password);
		
		if ( empty($errors) ) {
			// Check database to see if username and the hashed password exist there.
			$query = "SELECT id, username ";
			$query .= "FROM users ";
			$query .= "WHERE username = '{$username}' ";
			$query .= "AND hashed_password = '{$hashed_password}' ";
			$query .= "LIMIT 1";
			$result_set = mysql_query($query);
			confirm_query($result_set);
			if (mysql_num_rows($result_set) == 1) {
				// username/password authenticated
				// and only 1 match
				$found_user = mysql_fetch_array($result_set);
				$_SESSION['user_id'] = $found_user['id'];
				$_SESSION['username'] = $found_user['username'];
				
				
				redirect_to("juke.php");
				
			} else {
				// username/password combo was not found in the database
				$message = "Username/password combination incorrect.<br />
					Please make sure your caps lock key is off and try again.";
			}
		} else {
			if (count($errors) == 1) {
				$message = "There was 1 error in the form.";
			} else {
				$message = "There were " . count($errors) . " errors in the form.";
			}
		}
		
	} else { // Form has not been submitted.
		if (isset($_GET['logout']) && $_GET['logout'] == 1) {
			$message = "You are now logged out.";
		} 
		$username = "";
		$password = "";
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><title>Login please.</title></head><body>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<link type="text/css" href="css/custom-theme/jquery-ui-1.8.2.custom.css" rel="stylesheet" />	
<link type='text/css' href='css/login.css' rel='stylesheet' />
<script type="text/javascript" src="js/jquery-ui-1.8.2.custom.min.js">

</script>
<script type="text/javascript">
$(document).ready(function(){
$('*').css('borderTopLeftRadius','0px');
$('*').css('borderTopRightRadius','0px');
$('*').css('borderBottomRightRadius','0px');
$('*').css('borderBottomLeftRadius','0px');

$("form").submit(function(){

$('#lb').toggleClass('ui-state-active');
$('#lb').attr("value","Loading..");
});
});

</script>
<div style="margin:0px auto; width:500px;">
<table id="structure"  >
	<tr>
		
		<td id="page"><div class="ui-overlay"><div class="ui-widget-overlay"></div>
		<div class="ui-widget-shadow" style="position:absolute; top:200px; width:302px; height:225px;"></div></div>

				<div style="position:absolute; top:200px; height:203px; width:280px; padding: 10px;" class="ui-widget ui-widget-content">

									<h2 style="margin-top:0px; padding-bottom:10px; border-bottom:1px solid #545454;">User Login</h2>
			<?php if (!empty($message)) {echo "<p style='font-size:10px' class=\"message\">" . $message . "</p>";} ?>
			<?php if (!empty($errors)) { display_errors($errors); } ?>
			<form action="login.php" method="post">
			<table style="color:#fff">
				<tr>
					<td>Username:</td>
					<td><input style="width:180px" type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /></td>
				</tr>
				<tr>
					<td>Password:</td>
					<td><input style="width:180px" type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /></td>
				</tr>
				<tr>
					<td colspan="2"><input id='lb' class='ui-state-hover' style="float:right; margin-top:15px;" type="submit" name="submit" value="Login" /></td>
				</tr>
			</table>
			</form>

		
		
		</td>
	</tr>
</table>
</div>
</div></body></html>